<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2020/1/21
  Time: 15:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<script src="<%=request.getContextPath()%>/layui/layui.js" charset="utf-8"></script>
<script src="<%=request.getContextPath()%>/js/jquery-3.4.1.js"></script>
<script src="<%=request.getContextPath()%>/js/common.js"></script>
<body style="padding: 10px;background-color: rgb(236,240,245);">
    <h1 style="margin-bottom: 20px;">角色管理</h1>
    <input id="editOption" value="${editOption}" style="display: none">
    <!-- 功能区+表格 -->
    <div style="background-color: white;">
        <div class="layui-row">
            <!-- 新增按钮 -->
            <div class="layui-col-xs1" style="padding-left: 10px;padding-top: 10px;">
                <a href="roleAdd.jsp">
                    <button type="button" class="layui-btn" style="font-size: 1rem;"><i class="layui-icon layui-icon-add-1"></i>新增</button>
                </a>
            </div>
            <!-- 搜索框 -->
            <div class="layui-col-xs3 layui-col-xs-offset8" style="padding-top: 10px;padding-right: 10px;">
                <div style="width: 208px;height: 30px;border: 1px solid black;float: right;padding-left: 5px;">
                    <input id="txt_condition" placeholder="查找..." style="height: inherit;border: 0;"/>
                    <button type="button" class="layui-btn layui-btn-sm layui-bg-cyan" style="position: absolute;" onclick="queryRole()">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
        </div>
        <table class="layui-hide" id="tb_role" lay-filter="filter_role"></table>
        <!-- 表格工具列 -->
        <script type="text/html" id="bar_roleEdit">
            <a class="layui-btn layui-btn-xs layui-bg-gray" lay-event="set">
                <i class="layui-icon layui-icon-set-fill">设定</i>
            </a>
            <a class="layui-btn layui-btn-xs" lay-event="edit">
                <i class="layui-icon layui-icon-edit">修改</i>
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">
                <i class="layui-icon layui-icon-close">删除</i>
            </a>
        </script>
    </div>
</body>
<script>

    $(function () {
       //页面加载完成判断是否是从编辑页面跳转过来的
        isShowSuccessPanel();
    });

    // 表格模块
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem:'#tb_role',//在指定dom对象上渲染表格
            url:'/roleManage/getRoleListByPage.do',//异步数据接口
            where:{
                role_name:$("#txt_condition").val()//额外传递的参数
            },
            title: '角色表',
            cols: [[//表头
                {field:'role_id', title:'ID', hide:true},
                {field:'role_name', title:'名称', width:'70%'},
                {title:'操作', toolbar: '#bar_roleEdit', width:'30%'}
            ]],
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip','refresh'], //自定义分页布局
                groups: 5, //只显示 1 个连续页码
                first: false, //不显示首页
                last: false //不显示尾页
            }
        });

        //监听行工具事件
        //注：tool 是工具条事件名，filter_role 是 table 原始容器的属性 lay-filter="对应的值"
        table.on('tool(filter_role)', function(obj){

            var data = obj.data;//当行数据
            var event = obj.event;//按钮类型
            if(event == 'del'){
                layer.confirm('删除后无法恢复！是否删除？', function(index){

                    $.ajax({
                        url:'/roleManage/deleteRoleById.do',
                        data:{
                            role_id:data.role_id
                        },
                        dataType:'json',
                        error:function (xmlHttpRequest, textStatus, errorThrown) {//请求失败回调函数

                            layer.msg("删除失败！")
                        }
                    });
                    obj.del();//删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);//关闭弹出框
                });
            }else if(event == 'edit'){

                location.href = "/roleManage/toEditPage.do?role_id="+data.role_id;
            }else if(event == 'set'){

                location.href = "/jsp/roleManage/roleEdit.jsp?role_id="+data.role_id+"&role_name="+data.role_name;
            }
        });
    });

    /**
     * 查询角色方法
     */
    function queryRole() {
        //重载表格
        layui.table.reload('tb_role', {
            url: '/roleManage/getRoleListByPage.do'
            ,where: {
                role_name:$("#txt_condition").val()
            }
        });
    }

</script>
</html>
